package com.ddxz.demo.flowable.service.impl;

import com.ddxz.demo.flowable.dto.DdxzModelInfo;
import com.ddxz.demo.flowable.service.DdxzModelService;
import com.ddxz.demo.flowable.service.FlowService;
import com.ddxz.tool.web.dto.DdxzResponse;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Model;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/12/12
 */

@Service
public class FlowServiceImpl implements FlowService {
    @Override
    public DdxzResponse getModels(Integer pageNum, Integer pageSize) {
        List<Model> responses = repositoryService.createModelQuery()
                .listPage(pageSize, pageSize * (pageNum - 1));
        return DdxzResponse.correct(responses);
    }

    /**
     * 创建一个新的模型
     *
     * @param modelInfo 模型信息
     * @return 是否添加成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse addModel(DdxzModelInfo modelInfo) {
        Model model = repositoryService.newModel();
        model.setName(modelInfo.getName());
        model.setKey(modelInfo.getKey());
        model.setCategory(modelInfo.getDescription());
        modelService.saveModel(model);
        repositoryService.saveModel(model);
        return DdxzResponse.correct(model);
    }

    /**
     * 根据模型标识，获取一个模型信息
     *
     * @param modelId 模型标识
     * @return 模型信息
     */
    @Override
    public DdxzResponse getModelById(String modelId) {
        return modelService.getModelById(modelId);
    }

    @Override
    public DdxzResponse getAccount() {
        UserEntity user = new UserEntityImpl();
        user.setEmail("735129162@qq.com");
        user.setFirstName("书虫");
        user.setLastName("贾");
        user.setDisplayName("贾书虫");
        user.setId(USER);
        user.setTenantId("tenant");
        return DdxzResponse.correct(user);
//        UserRepresentation user = new UserRepresentation();
//        user.setEmail("735129162@qq.com");
//        user.setFirstName("书虫");
//        user.setLastName("贾");
//        user.setFullName("贾书虫");
//        user.setId("ddxz");
//        user.setTenantId("tenant");
//        user.setPrivileges(Arrays.asList(DefaultPrivileges.ACCESS_TASK, DefaultPrivileges.ACCESS_REST_API,
//                DefaultPrivileges.ACCESS_ADMIN, DefaultPrivileges.ACCESS_IDM, DefaultPrivileges.ACCESS_MODELER));
//        return DdxzResponse.correct(user);
    }

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private DdxzModelService modelService;
}
